//
//  ptbuyApp.swift
//  ptbuy
//
//  Created by MyMac on 2024/11/5.
//

import SwiftUI
import CoreData
import UIKit

@main
struct ptbuyApp: App {
   
    let persistenceController = PersistenceController.shared
    
    @StateObject private var monitor = NetworkMonitor()
    
    // 用来保存用户是否同意隐私协议
    @AppStorage(
        "hasAcceptedPrivacyPolicy"
    ) private var hasAcceptedPrivacyPolicy = false
    
    // SwiftUI的App生命周期
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    // 状态变量来控制屏幕方向
    static var orientationLock = UIInterfaceOrientationMask.portrait
    
    
    var body: some Scene {
        WindowGroup {
            /*
             测试页面
             */
//            Table()//表格测试页面
            //            SignatureView()//电子签名
            //            TextFieldSwiftUIView()//输入下啦提示框
            
            //            TestSwiftUIView()//下载进度提示框
            //            DataSwiftUIView().environment(
            //                                    \.managedObjectContext,
            //                                     persistenceController.container.viewContext
            //                                )//数据操作也main
            
            /* 
             正式页面
             */
            // 判断用户是否同意隐私协议
            if hasAcceptedPrivacyPolicy {
                MainPage().environment(
                    \.managedObjectContext,
                     persistenceController.container.viewContext
                ).environmentObject(monitor)
            }else{
                ContentView()

            }
//            WeChatShareButton()
//            SharePDFView()
        }
    }

}

//class AppDelegate: NSObject, UIApplicationDelegate {
//    static var orientationLock = UIInterfaceOrientationMask.portrait
//    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
//        return AppDelegate.orientationLock
//    }
//}
class AppDelegate: UIResponder, UIApplicationDelegate,ObservableObject{
    
    var window: UIWindow?
    
    // 保存方向锁定状态
    var orientationLock: UIInterfaceOrientationMask = .portrait

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return orientationLock
    }
    
    func lockOrientation(_ mask: UIInterfaceOrientationMask) {
        self.orientationLock = mask
    }
    
    func unlockOrientation() {
        self.orientationLock = .portrait // 默认竖屏
    }
}



